/*
 * This file is part of ELKI:
 * Environment for Developing KDD-Applications Supported by Index-Structures
 *
 * Copyright (C) 2019
 * ELKI Development Team
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package elki.similarity.kernel;

import elki.data.NumberVector;
import elki.distance.AbstractNumberVectorDistance;
import elki.similarity.AbstractVectorSimilarity;
import elki.utilities.Alias;
import elki.utilities.optionhandling.Parameterizer;
import elki.utilities.optionhandling.OptionID;
import elki.utilities.optionhandling.parameterization.Parameterization;
import elki.utilities.optionhandling.parameters.DoubleParameter;
import net.jafama.FastMath;

/**
 * Sigmoid kernel function (aka: hyperbolic tangent kernel, multilayer
 * perceptron MLP kernel).
 * 
 * @author Erich Schubert
 * @since 0.6.0
 */
@Alias("sigmoid")
public class SigmoidKernel extends AbstractVectorSimilarity {
  /**
   * Scaling factor c, bias theta
   */
  private final double c, theta;

  /**
   * Constructor.
   * 
   * @param c Scaling factor c.
   * @param theta Bias parameter theta.
   */
  public SigmoidKernel(double c, double theta) {
    super();
    this.c = c;
    this.theta = theta;
  }

  @Override
  public double similarity(NumberVector o1, NumberVector o2) {
    final int dim = AbstractNumberVectorDistance.dimensionality(o1, o2);
    double sim = 0.;
    for(int i = 0; i < dim; i++) {
      final double v = o1.doubleValue(i) * o2.doubleValue(i);
      sim += v;
    }
    return FastMath.tanh(c * sim + theta);
  }

  /**
   * Parameterization class.
   * 
   * @author Erich Schubert
   */
  public static class Par implements Parameterizer {
    /**
     * C parameter: scaling
     */
    public static final OptionID C_ID = new OptionID("kernel.sigmoid.c", "Sigmoid c parameter (scaling).");

    /**
     * Theta parameter: bias
     */
    public static final OptionID THETA_ID = new OptionID("kernel.sigmoid.theta", "Sigmoid theta parameter (bias).");

    /**
     * C parameter, theta parameter
     */
    protected double c = 1., theta = 0.;

    @Override
    public void configure(Parameterization config) {
      new DoubleParameter(C_ID, 1.) //
          .grab(config, x -> c = x);
      new DoubleParameter(THETA_ID, 0.) //
          .grab(config, x -> theta = x);
    }

    @Override
    public SigmoidKernel make() {
      return new SigmoidKernel(c, theta);
    }
  }
}
